﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Windows.Forms;
using Database;
using Communication;
//using WCFClient;

namespace WindowsFormsApplication1
{
    public class ForumService
    {
        User me = null;
        int forum;
        FormMain mainForm;
        FormUserList userListForm;
        ClientCommunication clientComs;
        SortedDictionary<int, string> forumItems;
        Thread thread;
        SubForum sub;
        FormRegister regForm;


        public ForumService(FormMain formMain)
        {
            this.forum = 114;
            this.mainForm = formMain;
            this.clientComs = new ClientCommunication(this, forum);
        }



        public void logout()
        {
            this.clientComs.logout(me);
        }


        public void getUserList(FormUserList ful)
        {
            this.userListForm = ful;
            clientComs.getUserList(me);
        }


        //----getters--------
        public void getMainForum()
        {
            this.clientComs.getMainForum(me);
        }

        public void getSubForum(int pos)
        {
            ClientRequest temp = new ClientRequest(this.me,this.forum);
            temp.setSubForumId( this.forumItems.ElementAt(pos).Key);
            this.clientComs.getSubForum(temp);
        }

        public void getThread(int pos)
        {
            ClientRequest temp = new ClientRequest(this.me, this.forum);
            temp.setSubForumId(this.sub.getId());
            temp.setThreadId(this.sub.getThreads().Values.ToList()[pos].getId());
            clientComs.getThread(temp);
        }


        //-----promotes
        public void promote(string name, string forum)
        {
            this.clientComs.promote(me, name, forum);
        }



        public void CreateForum(User manager, string forum, int mods, int modCan, int modsDel, int notific, int mailAu, List<string> badWords)
        {
            this.clientComs.AddForum(me, manager, forum, mods, modCan, modsDel, notific, mailAu, badWords);
        }


        internal void exchangeModerator(string oldMod, string newMod, string forum)
        {
            this.clientComs.exchangeModerator(me, oldMod, newMod, forum);
        }

        public void demote(string name, string forum)//2 bet
        {
            this.clientComs.demote(me, name, forum);
        }

        //------user loggins------

        public string register(string name, string last, string nick, string pass, string email, FormRegister register)
        {
            this.regForm = register;
            clientComs.register(name, last, nick, pass, email);
            return "register " + name + " " + last + " " + nick + " " + pass + " " + email;
        }

        public void login(string flag, string user, string password)//flag for guest or login, user for user name
        {
            System.Text.UTF8Encoding UTFEncoder = new System.Text.UTF8Encoding();
            Byte[] encr = UTFEncoder.GetBytes(password);
            this.clientComs.login(flag, user, encr);
        }



        //---forum actions--------
        public void postReply(string title, string content)
        {
            ClientRequest temp = new ClientRequest(this.me, title, content,this.forum);

            this.clientComs.postReply(temp);
        }

        public void newThread(string title, string content)
        {
            ClientRequest temp = new ClientRequest(this.me, title, content, this.forum);
            this.clientComs.newThread(temp);
        }

        public void newSub(string title)
        {
            this.clientComs.newSub(new ClientRequest(me, title,forum));
        }

       

        public void updateThread(string title, string content, int pos)//1 bet
        {
            ClientRequest temp = new ClientRequest(this.me, title, content, this.forum);
            temp.setThreadId(this.sub.getThreads().ElementAt(pos).Value.getId());

            this.clientComs.editThread(temp);
        }

        public void deleteThread(int pos)//1 gimel
        {
            ClientRequest temp = new ClientRequest(this.me, this.forum);
            temp.setThreadId(this.sub.getThreads().ElementAt(pos).Value.getId());

            this.clientComs.deleteThread(temp);
        }


        public void deletePost(int pos)//1 gimel
        {
            ClientRequest temp = new ClientRequest(this.me, this.forum);
            temp.setPostId(this.thread.getPosts().ElementAt(pos-1).Value.getID());

            this.clientComs.deletePost(temp);
        }

       

        public void deleteSubForum(int pos)//2 hei
        {
            ClientRequest temp = new ClientRequest(this.me, this.forum);
            temp.setSubForumId(this.forumItems.ElementAt(pos).Key);

            this.clientComs.deleteSubForum(temp);
        }



        internal void editPost(string title, string content, int pos)
        {
            ClientRequest temp = new ClientRequest(this.me, title, content, this.forum);
            temp.setPostId(thread.getPosts().ElementAt(pos-1).Value.getID());
            
            this.clientComs.editPost(temp);
        }

        internal void editThread(string title, string content, int pos)
        {
            ClientRequest temp = new ClientRequest(this.me, title, content, this.forum);
            temp.setThreadId(this.sub.getThreads().ElementAt(pos).Value.getId());

            this.clientComs.editThread(temp);
        }


        //----stats requests-----------------------
        public void getForumStats()//2 vav (OPTIONAL)
        {
            this.clientComs.getForumStats(me);
        }

        
       
        internal void getAdminAccess(string user, string password)
        {
            this.clientComs.getForumsStats(user, password);
        }


        internal void confirmCode(string name, string last, string nick, string pass, string email, string text)
        {
            this.clientComs.confirmCode(name, last, nick, pass, email, text);
        }

        // Callback---------------------------------------------------------------------------------------------------

        public void OnMessageAdded(string message, DateTime timestamp)
        {
            throw new NotImplementedException();
        }

        //public void receivePage(string[] lines,string header)
        //{

        //    /*
        //    SortedDictionary<int, Common.DataBase.Message> messages = page.messages;
        //    string[] strArr = new string[10];
        //    foreach (int msg in messages.Keys)
        //    {
        //        Common.DataBase.Message tmpMsg = messages[msg];
        //        strArr[msg] = "(" + msg + ")" + "\tTitle: " + tmpMsg.Title + ":" + tmpMsg.Body;
        //    }
        //    */

        //    this.mainForm.updateMainPanel(lines,header);

        //}

        public void receiveJoin(User client)
        {
            this.me = client;
            getMainForum();
            this.mainForm.loadUserName(client.getUserName());
        }


        public void receiveUsersList(List<User> uList)
        {
            this.userListForm.updateUserList(uList);
        }

        public void autherizationError()
        {
            new FormError("You don't have autherization to do this action!").Show();
        }

        public void RecieveError(string error)
        {
            new FormError(error).Show();
        }

        internal void receiveUpdateList(string upmessage)
        {
            this.mainForm.updateUpdatePanel(upmessage);
        }



        internal void receiveAdminAccess(string stats, User me)
        {
            this.me = me;
            new FormAdminAccess2(stats, this).Show();
        }

        internal void receiveForum(Forum forum)
        {
            this.forumItems = forum.forumToString();
            this.mainForm.loadMainForum(this.forumItems.Values.ToList(), forum.getTitle() + "\n" + forum.getAdmin().getUserName());
        }

        internal void receiveSubForum(SubForum subforum)
        {
            this.sub = subforum;
            this.forumItems = subforum.subForumToString();
            this.mainForm.loadSubForum(this.forumItems.Values.ToList(), subforum.getTitle() + "\n" + subforum.SFModeratorsToString());
        }

        internal void receiveThread(Thread thread)
        {
            this.thread = thread;
            this.forumItems = thread.threadToString();
            this.mainForm.loadThread(this.forumItems.Values.ToList(), thread.getTitle());
        }



        
    
        internal void ReceiveForumStatistics(string stats)
        {
 	        new FormInfo(stats).Show();
        }

        internal void adminExit()
        {
            this.clientComs.adminExit(me);
        }


        public bool validatePost(string title, string body)
        {
            if (title.Length <= 50 && body.Length <= 4000)
                return true;
            else
            {
                new FormError("a Title can't be longer than 50 characters and the body can't be longer than 4000 characters").Show();
                return false;
            }
        }





        internal void recieveCodeConfirm()
        {
            this.regForm.Dispose();
        }
    }
}
